package com.test.deencredit.analytics_deencredit.util_deencredit

import android.app.Application
import android.content.Context
import com.test.deencredit.analytics_deencredit.bean_deencredit.PamHitPointdeencredit_deencredit
import com.test.deencredit.analytics_deencredit.event_deencredit.IAnalyticsEvent_deencredit
import com.test.deencredit.analytics_deencredit.http_deencredit.HttpHelper_deencredit
import com.google.gson.Gson
import com.test.deencredit.config_deencredit.CountryConfig_deencredit

object AnalyticsUtil_deencredit {

    /**
     * 初始化分析模块
     *
     * @param context Application
     */
    fun init_deencredit(context: Application) {
        /** 初始化google install referrer */
        GoogleInstallUtil_deencredit.init_deencredit(context) {
            /** 保存渠道信息*/
            saveGoogleInstallInfo_deencredit(mutableListOf(CountryConfig_deencredit.getInstance_deencredit().commonPath_deencredit))
        }

        /** 初始化广告id */
        AdvertIdUtil_deencredit.init_deencredit(context) { _, _ ->
            /** 保存渠道信息*/
            saveGoogleInstallInfo_deencredit(mutableListOf(CountryConfig_deencredit.getInstance_deencredit().commonPath_deencredit))
        }

        /** 初始化设备信息 */
        SycDeviceUtil_deencredit.init_deencredit(context)

        /** 初始化adjust信息 */
        AdjustUtil_deencredit.init_deencredit(context)

        /** 初始化facebook */
        FacebookUtil_deencredit.init_deencredit(context)

        /** 初始化评分模块 */
        ApRvUtil_deencredit.iniAprvutil_deencredit(context)

    }

    /**
     * 更新设备模块服务器请求地址
     * 1、如果未登录，在登录页登录成功后触发
     * 2、如果已登录，在启动时登录地区的转发路径时初始化成功后触发
     *
     * @param context Application
     */
    fun initDevice_deencredit(context: Application) {
        /** 初始化设备信息 */
        SycDeviceUtil_deencredit.init_deencredit(context)
    }

    /**
     * 打点
     * 主要对注册、申贷这两个结果记录打点
     * 均是接口返回成功后触发打点
     *
     * @param event 事件名
     */
    fun addEvent_deencredit(event: IAnalyticsEvent_deencredit) {
        AdjustUtil_deencredit.addEvent_deencredit(event)
    }

    /**
     * 保存google s2s信息
     * **通常不用手动触发**
     * 1、启动页，触发保存
     */
    fun saveGoogleInstallInfo_deencredit(targetUrlList: List<String>) {
        GoogleInstallUtil_deencredit.saveS2sInfo_deencredit(targetUrlList)
    }

    /**
     * 保存facebook s2s信息
     * 1、启动页，如果是已登录状态，触发保存
     * 2、登录页，登录成功后，触发保存
     */
    fun saveFacebookS2SInfo_deencredit(targetUrlList: List<String>) {
        FacebookUtil_deencredit.saveS2SInfo_deencredit(targetUrlList)
    }

    /**
     * 保存Device s2s信息
     * 1、首页，点击申请且所有权限通过后，触发保存
     */
    fun saveDeviceS2SInfo_deencredit() {
        SycDeviceUtil_deencredit.saveS2SInfo_deencredit()
    }

    /**
     * 保存Appsflyer 安装信息
     * **通常不用手动触发**
     * 1、收到Appsflyer的转化回调后，触发保存
     */
    fun saveAppsflyerInstall_deencredit(targetUrlList: List<String>) {
//        AppsflyerUtil.saveInstallInfo(targetUrlList)
    }

    /**
     * 保存Adjust 安装信息
     * **通常不用手动触发**
     * 1、收到Adjust的转化回调后，触发保存
     */
    fun saveAdjustInstall_deencredit(targetUrlList: List<String>) {
        AdjustUtil_deencredit.saveInstallInfo_deencredit(targetUrlList)
    }

    /**
     * 保存三方库 s2s信息
     * 1、启动页，如果是已登录状态，触发保存
     * 2、登录页，登录成功后，触发保存
     */
    fun saveFirebaseAndFacebookS2SInfo_deencredit() {
        saveFacebookS2SInfo_deencredit(mutableListOf(CountryConfig_deencredit.getInstance_deencredit().commonPath_deencredit))
    }

    /**
     * 保存UsageStats s2s信息
     * **通常不用手动触发**
     * 1、首页点击申请时触发，前提是用户已授权
     */
    fun saveUsageStatsS2SInfo_deencredit(context: Application, targetUrlList: List<String>) {
//        UsageStatsUtil.saveS2SInfo(context, targetUrlList)
    }

    /**
     * 保存事件埋点
     *
     * @param type 事件类型
     * @param remark 备注
     */
    fun saveHitPointInfo_deencredit(type: String, remark: String) {
        try {
            val params = PamHitPointdeencredit_deencredit(type = type, remark = remark)
            val body = Gson().toJson(params).trim()
            HttpHelper_deencredit.postData_deencredit("", body)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 判断是否拥有使用统计权限
     * 若拥有权限，则自动上传数据
     * 若没有权限，直接回调
     *
     * @param context Application
     * @param permissionGrantListener 权限授权回调
     */
    fun checkUsageStatsPermission_deencredit(
        context: Application,
        permissionGrantListener: (Boolean) -> Unit
    ) {
//        UsageStatsUtil.checkAppUsagePermission(context, permissionGrantListener)
    }

    /**
     * 跳转使用统计权限页
     *
     * @param context 上下文
     */
    fun startToUsageStatsPermissionSettingPage_deencredit(context: Context) {
//        UsageStatsUtil.startToUsageStatsPermissionSettingPage(context)
    }

    /**
     * 获取谷歌安装渠道信息
     */
    fun getInstallReferrer_deencredit(): String {
        return GoogleInstallUtil_deencredit.getInstallReferrer_deencredit()
    }

    /**
     * 获取adId
     */
    fun getAdId_deencredit(): String {
        return AdvertIdUtil_deencredit.getAdId_deencredit()
    }

    /**
     * 获取adEnabled
     */
    fun getAdEnabled_deencredit(): Int {
        return AdvertIdUtil_deencredit.getAdEnabled_deencredit()
    }

    /**
     * 获取安卓Id
     */
    fun getAndroidId_deencredit(): String {
        return AdvertIdUtil_deencredit.getAndroidId_deencredit()
    }

}